package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.PollutionControl;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by maowenjie on 2017/3/15.
 *治污中心-----Dao层
 */
@Repository
public class PollutionControlDao extends MyBatisDao<PollutionControl>{
    /**
     *
     * @param id 主 键
     * @return 状态
     */
    public Integer findStatusById(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }

    public Paging<PollutionControl> pagingPollutionControls(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(sqlId("countGovernment"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<PollutionControl> data = getSqlSession().selectList(sqlId("pagingGovernment"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 养殖端合计
     */
    public Map<String, Float>  aquacultureSum(Map<String, Object> criteria) {
        Float dryUsedQuantity = getSqlSession().selectOne(sqlId("drysum"),criteria);
        Float liquidUsedQuantity = getSqlSession().selectOne(sqlId("liquisum"),criteria);
        Map<String , Float> map = Maps.newHashMap();
        if(dryUsedQuantity == null){
            map.put("dry",0f);
        }else {
            map.put("dry",dryUsedQuantity);
        }
        if(liquidUsedQuantity == null){
            map.put("liquid",0f);
        }else {
            map.put("liquid",liquidUsedQuantity);
        }
        return map;
    }

    /**
     * 政府端合计
     */
    public Map<String, Float>  governmentSum(Map<String, Object> criteria) {
        Float dryUsedQuantity = getSqlSession().selectOne(sqlId("gmtdrySum"),criteria);
        Float liquidUsedQuantity = getSqlSession().selectOne(sqlId("gmtliquiSum"),criteria);
        Map<String , Float> map = Maps.newHashMap();
        if(dryUsedQuantity == null){
            map.put("dry",0f);
        }else {
            map.put("dry",dryUsedQuantity);
        }
        if(liquidUsedQuantity == null){
            map.put("liquid",0f);
        }else {
            map.put("liquid",liquidUsedQuantity);
        }
        return map;
    }

    /**
     * 返回当前单据的编码的最大值
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }

    public Map<String , Object> sumForWeb(Map<String, Object> map) {

        return getSqlSession().selectOne(sqlId("sumForWeb"),map);
    }
}

